//
//  TopicListTableViewController.m
//  HomeDecoration
//
//  Created by xalo on 16/2/26.
//  Copyright © 2016年 美家项目组. All rights reserved.
//
<<<<<<< HEAD

#import "TopicListTableViewController.h"
#import "TopicTableViewCell.h"
@interface TopicListTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) TopicTableViewCell *topicCell;


=======
#define kViewWidth self.view.bounds.size.width



#import "TopicListTableViewController.h"
#import "TopicTableViewCell.h"
#import <RealReachability/RealReachability.h>
#import "TopicHelper.h"
#import "TopicModel.h"
#import "ShufflingFigureHelper.h"
#import "UIImageView+WebCache.h"
@interface TopicListTableViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;  //轮播图滚动视图

@property (nonatomic,strong) UIPageControl *pageControl;  //页数标签

@property (nonatomic,strong) NSMutableArray *imageArray;  //轮播图图片

//@property (nonatomic, assign) BOOL hidden;
//@property (nonatomic, assign) BOOL scrollUporDown;
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18
@end

@implementation TopicListTableViewController

<<<<<<< HEAD
- (void)viewDidLoad {
    [super viewDidLoad];
=======
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;  

}


- (void)viewDidLoad {
    [super viewDidLoad];    
    
    [GLobalRealReachability startNotifier];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(networkChanged:)
                                                 name:kRealReachabilityChangedNotification
                                               object:nil];
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    [self addScrollViewToTableHeaderView];
    
<<<<<<< HEAD
    
}

#pragma mark --- 添加轮播图
-(void)addScrollViewToTableHeaderView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    scrollView.contentSize = CGSizeMake(5 * kWidth, 200);
    
    scrollView.pagingEnabled = YES;
    
    for (int  i=0; i<5; i++) {
        NSString *name = [NSString stringWithFormat:@"4.jpg"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, 200)];
        UIImage *image = [UIImage imageNamed:name];
        imageView.image = image;
        [scrollView addSubview:imageView];
    }
    
    self.tableView.tableHeaderView = scrollView;
}

=======
    self.imageArray = [NSMutableArray array];
    
    self.imageArray = [[ShufflingFigureHelper sharedManager] returnImageArray];
    
    NSLog(@"%@",self.imageArray);
    
}

//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if ([scrollView isEqual:self.tableView]) {
//        
//        static float newy = 0;
//        static float oldy = 0;
//        newy = scrollView.contentOffset.y;
//        
//        if (newy != oldy ) {
//            if (newy > oldy) {
//                self.scrollUporDown = YES;
//            }else if(newy < oldy){
//                self.scrollUporDown = NO;
//            }
//            oldy = newy;
//        }
//        if (_scrollUporDown == YES) {
//            self.hidden = YES;
//            
//            [UIView animateWithDuration:1 animations:^{
//                
//                self.navigationController.navigationBar.frame = CGRectMake(0, -40, kViewWidth, 40);
//            }];
//            
//        }
//        else if (_scrollUporDown == NO) {
//            
//            if (self.hidden == NO) {
//                
//                [UIView animateWithDuration:1 animations:^{
//                    
//                    self.navigationController.navigationBar.frame = CGRectMake(0, 20, kViewWidth, 40);
//                }];
//                self.hidden = YES;
//            }
//        }
//        
//    }
//}


#pragma mark --- 检测网络状态实现方法
- (void)networkChanged:(NSNotification *)notification
{
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    
    if (status == RealStatusNotReachable)
    {
        [[[UIAlertView alloc] initWithTitle:@"RealReachability" message:@"无网络,不能上网" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil , nil] show];
    }
    
    if (status == RealStatusViaWiFi)
    {
        [[TopicHelper sharedManager] fetchDataWithUrl:kTopic block:^{
           
            [self.tableView reloadData];
        }];
        
        [[ShufflingFigureHelper sharedManager] fetchImageWithUrl:kTopicPlayImage block:^{
            
            [self.tableView reloadData];
            
        }];
    }
    if (status == RealStatusViaWWAN)
    {
        [[[UIAlertView alloc] initWithTitle:@"RealReachability" message:@"收费网" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil , nil] show];
    }
}


#pragma mark --- 添加轮播图
-(void)addScrollViewToTableHeaderView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    _scrollView.contentSize = CGSizeMake(5 * kWidth, 200);
    
    _scrollView.pagingEnabled = YES;
    
    for (int  i=0; i<6; i++) {
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, 200)];
        
//        [imageView sd_setImageWithURL:[NSURL URLWithString:str]];
        
//        UIImage *image = [UIImage imageNamed:[NSURL URLWithString:str]];
//        imageView.image = image;
        [_scrollView addSubview:imageView];
    }
    self.tableView.tableHeaderView = _scrollView;
    
    //1.创建
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 170, self.view.frame.size.width - 200, 20)];
    //2.设置属性
    _pageControl.numberOfPages = 5;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    
    //3.添加点击事件
    [_pageControl addTarget:self action:@selector(changeContentOffSet:) forControlEvents:UIControlEventValueChanged];
    
    //4.添加
    [self.tableView addSubview:_pageControl];

    //三.使用计时器  NSTimer    实现自动轮播
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
}

//点击pageControl 改变偏移量
-(void)changeContentOffSet:(UIPageControl *)pageControl
{
    //1.获取 pageControl 当前页面
    NSInteger currentPage = pageControl.currentPage;
    //2.让 currentPage 与 scrollView 页面对应
    CGFloat offSetX = _scrollView.frame.size.width * (currentPage + 1);
    //3.更改 scrollView 的偏移量 并作动画
    [_scrollView setContentOffset:CGPointMake(offSetX,0) animated:YES];
}

#pragma mark --- 自动轮播
//autoPlay
-(void)autoPlay
{
    //1.获取 scrollView 的偏移量
    CGFloat offSetX = _scrollView.contentOffset.x;
    
    CGFloat width = _scrollView.frame.size.width;
    //2.更改偏移量
    if (offSetX + width > 5 * width) {
        // 大于等于4 * width  说明是右边界, 先置为(0,0),在做动画
        [_scrollView setContentOffset:CGPointZero animated:NO];
        //3.在做动画
        [_scrollView setContentOffset:CGPointMake(width, 0) animated:YES];
        
    }else{
        [_scrollView setContentOffset:CGPointMake(offSetX + width, 0) animated:YES];
    }
}

//更改内容偏移量
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat width = _scrollView.frame.size.width;
    //1.获取偏移量  X
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    //2.判断
    if (contentOffSetX < 0) {
        //小于0  说明是左边界,重置偏移量为  (5 * width , 0)
        [scrollView setContentOffset:CGPointMake(5 * width, 0) animated:NO];
        
    }else if (contentOffSetX > width * 5) {
        //大于5 * width   说明是右边界,重置偏移量为(0,0)
        [scrollView setContentOffset:CGPointZero animated:NO];
    }
    
}

//滚动视图时,让 视图 与 pageControl 对应
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //1. 获取偏移量
    CGFloat offSetX = _scrollView.contentOffset.x;
    
    CGFloat width = _scrollView.frame.size.width;
    
    //2.计算当前图片
    NSInteger number = offSetX / width;
    
    //3.让 pageControl 与 number 对应
    
    if (number != 0) {
        _pageControl.currentPage = number - 1;
    }else if(number == 0)
    {
        _pageControl.currentPage = 4;   //numberOfPage - 1,   最后一个点
    }
}

#pragma mark ---- 动画结束后更改 pageControl
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //1. 获取偏移量
    CGFloat offSetX = _scrollView.contentOffset.x;
    
    CGFloat width = _scrollView.frame.size.width;
    
    //2.计算当前图片
    NSInteger number = offSetX / width;
    
    //3.让 pageControl 与 number 对应
    
    if (number != 0) {
        _pageControl.currentPage = number - 1;
    }else if(number == 0)
    {
        _pageControl.currentPage = 4;   //numberOfPage - 1,   最后一个点
    }
    
}
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
<<<<<<< HEAD
#warning Incomplete implementation, return the number of sections
=======

>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
<<<<<<< HEAD
#warning Incomplete implementation, return the number of rows
    return 5;
=======
    
    return [[TopicHelper sharedManager] modelArrayForCount];
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicCell" forIndexPath:indexPath];
    
<<<<<<< HEAD
    // Configure the cell...
=======
    TopicModel *model = [[TopicHelper sharedManager] returnOfModelWithIndex:indexPath.row];
    
    [cell setContentViewWithModel:model];
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
<<<<<<< HEAD
//    CGRect bound = [self.articleCell.contentLabel.text boundingRectWithSize:CGSizeMake(self.articleCell.frame.size.width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName] context:nil];
    
    return 250;
}




=======
    return 250;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.navigationController.navigationBarHidden = NO;
    
    TopicModel *model = [[TopicHelper sharedManager] returnOfModelWithIndex:indexPath.row];
    
    UIViewController *viewControl = [[UIViewController alloc] init];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[kTopicDetail stringByAppendingString:[NSString stringWithFormat:@"%ld",model.Id]]]];
    
    [webView loadRequest:request];
    
    [viewControl.view addSubview:webView];
    
    [self.navigationController pushViewController:viewControl animated:YES];
}
>>>>>>> 1975201db659495b68bacbc5bddd203a4c2ada18








/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
